package com.shirley.customcontrols;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shirley.wordgrouporderview.WordGroupOrderView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ZLJ on 2017/12/18.
 * 主界面的Tab2对应的fragment
 */

public class WordGroupOrderFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R.id.word_group_order_view)
    WordGroupOrderView wordGroupOrderView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_word_group_order, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        wordGroupOrderView.setNeedRealIndex(true)
                .setData(readWordFromRaw());
        /*wordGroupOrderView.setOnItemClickListener(new WordGroupOrderView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, String data) {
                Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show();
            }
        });*/
    }

    /**
     * 从raw中读取word.txt
     */
    private List<String> readWordFromRaw() {
        List<String> words = new ArrayList<>();
        try {
            InputStream is = getResources().openRawResource(R.raw.word);
            InputStreamReader reader = new InputStreamReader(is);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                words.add(str.split(" ")[0]);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return words;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
